package com.hnyyac.ad.mds.views_notload

import android.content.Context
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.*
import com.hnyyac.ad.mds.GMAds
import com.hnyyac.ad.mds.utils.UIUtils


class PangleBannerAdHelper(
    private val context: Context,
    private val mediaId: String,
    private val container: ViewGroup,
    private val position:Int
) {
    private var bannerAd: TTNativeExpressAd? = null
    // 初始化广告加载器
    private val adNativeLoader: TTAdNative by lazy {
        TTAdSdk.getAdManager().createAdNative(context)
    }
    // 加载Banner广告
    fun loadBannerAd() {

        val adSlot = AdSlot.Builder()
            .setCodeId(mediaId) // 广告位ID
            .setImageAcceptedSize(
                UIUtils.getScreenWidthInPx(context),
                UIUtils.dp2px(context, 340f)
            ) // 自渲染尺寸，单位px
            .setExpressViewAcceptedSize(
                UIUtils.getScreenWidthDp(context),
                0f
            ) // 模板广告尺寸，单位dp
            .build()
        GMAds.bannerListener?.onTrigger(mediaId)
        adNativeLoader.loadBannerExpressAd(adSlot, object : TTAdNative.NativeExpressAdListener {
            override fun onError(errorCode: Int, errorMsg: String) {
                Log.d("PangleBanner", "Load failed: $errorCode - $errorMsg")
                GMAds.bannerListener?.onError(errorMsg,mediaId)
            }

            override fun onNativeExpressAdLoad(ads: MutableList<TTNativeExpressAd>?) {
                if (ads.isNullOrEmpty()) {
                    Log.d("PangleBanner", "Load success but ad list is empty")
                    return
                }

                bannerAd = ads[0]
                GMAds.bannerListener?.onLoad(mediaId)
                showBannerAd()
            }
        })
    }

    // 展示Banner广告
    private fun showBannerAd() {
        bannerAd?.let { ad ->
            // 设置广告交互监听
            ad.setExpressInteractionListener(object : TTNativeExpressAd.ExpressAdInteractionListener {
                override fun onAdClicked(view: View?, type: Int) {
                    GMAds.bannerListener?.onAdClick(bannerAd!!,mediaId)
                }

                override fun onAdShow(view: View?, type: Int) {
                    GMAds.bannerListener?.onAdShow(bannerAd!!,mediaId,position)
                }

                override fun onRenderFail(view: View?, errorMsg: String?, errorCode: Int) {
                    //Log.d("PangleBanner", "Render failed: $errorCode - $errorMsg")
                    GMAds.bannerListener?.onError(errorMsg!!,mediaId)
                }

                override fun onRenderSuccess(view: View?, width: Float, height: Float) {
                   // Log.d("PangleBanner", "Render success")
                    GMAds.bannerListener?.onSuccess()
                }
            })

//            // 设置不喜欢按钮回调
//            ad.setDislikeCallback(context, object : TTAdDislike.DislikeInteractionCallback {
//                override fun onSelected(position: Int, value: String?, enforce: Boolean) {
//                    Log.d("PangleBanner", "Banner closed")
//                    container.removeAllViews()
//                }
//
//                override fun onCancel() {}
//                override fun onShow() {}
//            })

            // 获取广告View并添加到容器
            ad.expressAdView?.let { adView ->
                container.removeAllViews()
                container.addView(adView)
            }
        } ?: run {
            Log.d("PangleBanner", "Please load ad first or wait for ad loading complete")
        }
    }




    // 销毁广告
    fun destroy() {
        bannerAd?.destroy()
        container.removeAllViews()
        bannerAd = null
    }
}